function $(id) {
  var obj = document.getElementById(id)
  if(obj) {
    obj.show = function() {
      obj.style.display = 'inline'
    }
    obj.hide = function() {
      obj.style.display = 'none'
    }
  }
  return obj
}

function $$(tagName) {
  return document.getElementsByTagName(tagName)
}

function openLink(link) {
  if(window.widget) {
    window.widget.openURL(link.href)
  }else {
    alert(link.href)
  }
}

function initButton() {
  if (window.widget) {
    new AppleGlassButton($("doneButton"), "Done", hidePrefs);
    var gInfoButton = new AppleInfoButton($("infoButton"), $("front"), "white", "white", showPrefs);
    gInfoButton.setStyle("black", "black");
  }else {
    $('infoButton').innerHTML = '<input type="button" value="i" onclick="showPrefs()"/>'
    $('doneButton').innerHTML = '<input type="button" value="Done" onclick="hidePrefs()"/>'
  }
}

function handleCommand(command) {
  var output = command.outputString
  if(!output) {
    log("Error: " + command.errorString)
    return false;
  }
  return output
}

function displayVersion() {
  var command = window.widget.system('./ccm -v', function() {
    var output = handleCommand(command)
    if(output) {
      $('version-span').innerHTML = output
    }
  });
}

function updateVersion() {
  var command = window.widget.system('./ccm -u', function() {
    var output = handleCommand(command)
    if(output && !output.match('No update.')) {
      try {
        displayVersionUpdateInfo(eval(command.outputString))
      } catch (e) {
        log("Can't parse version info: " + e);
      }
    }
  });
}

function displayVersionUpdateInfo(remoteVersion) {
  if(remoteVersion) {
    $('version-span').innerHTML = "<a href='" + remoteVersion.link + "' title='CC Monitor version" + version.number + ' is ready, click to download it. ' + (version.description || '') + "' target='blank' onclick='openLink(this)'><img src='images/dance.gif' class='spinner'/></a>";
    $('version-span').style.top = '5px'
  }
}

function initConfiguration() {
  if(window.widget) {
    var source = window.widget.preferenceForKey(KEY_CONFIGURATION_SOURCE)
    if(source) {
      $('configuration_source').value = source;
    }
    
    var interval = window.widget.preferenceForKey(KEY_INTERVAL);
    if(interval >= 15) {
      $('interval').value = interval
    }
    
    var ccmonitor_uri = window.widget.preferenceForKey(KEY_MONITOR_URI);
    if(ccmonitor_uri) {
      $('ccmonitor_uri').value = ccmonitor_uri
    }
    
    var local_monitor = window.widget.preferenceForKey(KEY_LOCAL_MONITOR);
    if(local_monitor) {
      setLocalMonitorValue(local_monitor)
    }
    
    if(source) {
      if(source == $('monitor_selection_uri').value) {
        $('monitor_selection_uri').checked = true;
      }
      if(source == localCcMonitor) {
        $('monitor_selection_local').checked = true;
      }
    }
  }
}

function saveConfiguration() {
  if(window.widget) {
    window.widget.setPreferenceForKey($('configuration_source').value, KEY_CONFIGURATION_SOURCE);
    window.widget.setPreferenceForKey($('interval').value, KEY_INTERVAL);
    window.widget.setPreferenceForKey($('ccmonitor_uri').value, KEY_MONITOR_URI);
    window.widget.setPreferenceForKey(getLocalMonitorValue(), KEY_LOCAL_MONITOR);
  }
  setConfiguration();
}

function getMonitorSelection() {
  var selections = document.getElementsByName('monitor_selection')
  for(var i=0, len=selections.length; i<len; i++) {
    if(selections[i].checked == true) {
      return selections[i];
    }
  }
}

function selectedValue(selected) {
  return (selected && strip($(selected.value).value).length > 0) ? getSourceValule(selected.value) : ''
}

function getSourceValule(value) {
  return value == 'ccmonitor_uri' ? $(value).value : localCcMonitor
}

function interval() {
  try {
    return parseInt($('interval').value)
  }catch(e) {
    return 0
  }
}

function strip(str) {
  if(str == undefined || str == null) {
    return ''
  }
  return str.replace(/^\s+/, '').replace(/\s+$/, '');
}

function redrawSpinners() {
  imgs = $$('img')
  for(var i=0, len=imgs.length; i<len; i++) {
    if(imgs[i].src.match('spinner.gif')) {
      var tmp = imgs[i].src
      imgs[i].src = ''
      imgs[i].src = tmp
    }
  }
}
